package controllers

import play.api._
import play.api.mvc._
import models.Task
import scala.collection.mutable.ArrayBuffer
import com.codahale.jerkson.Json
import play.api.cache.Cache
import play.api.cache.Cached
import play.api.Play.current
import org.joda.time.DateTime

object Application extends Controller {

  val devs = ArrayBuffer("Tony Zhang",
    "Lei Zhang",
    "Yolanda Liu",
    "Ben An",
    "Terry Wang",
    "Wayne Wang",
    "Adward Tu",
    "Frank Li",
    "Gordon Cheng",
    "Morphy Tong",
    "Gary Chen",
    "Lucien Wang")

  def index = Cached("Homepage") {
    Action {
      Ok(views.html.index(Task.all()))
    }
  }

  def devTask(name: String) = {
    Action {
      Ok(views.html.index(Task.byResourceWithParent(name)))
    }
  }

  def taskInPeriod() = Action {
    val now = new DateTime()
    val startDate = now.plusDays(-7)
    val endDate = now.plusDays(7)
    Ok(views.html.period(Task.byPeriod(startDate, endDate), startDate.toLocalDate.toString, endDate.toLocalDate.toString))
  }

  def taskThisMon() = Action {
    Ok(views.html.index(Task.inPeriod(new DateTime())))
  }
  // JSON service
  def listBars(name: String) = Action {
    val bars = Task.byResource(name)

    val json = Json.generate(bars)

    Ok(json).as("application/json")
  }

}